package com.example.hotelsystem.controller.room;


import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.entity.vo.RoomSearchVO;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.*;
import com.example.hotelsystem.utils.ExcelUtils;
import com.example.hotelsystem.vo.room.RoomSaveVO;
import com.example.hotelsystem.vo.room.RoomViewVO;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.format.annotation.DateTimeFormat;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/hotelsystem/room")
@Api(tags = "房间控制器")
public class RoomController {
    @Autowired
    CityService cityService;
    @Autowired
    HotelNameService hotelNameService;
    @Autowired
    FloorService floorService;
    @Autowired
    RoomService roomService;
    @Autowired
    RoomTypeService roomTypeService;
    @Autowired
    TDictService tDictService;

    @ApiOperation("导出待打扫的房间")
    @GetMapping("/export")
    public Result exportClean(HttpServletResponse response,RoomSearchVO roomSearchVO){
            QueryWrapper<Room> query = new QueryWrapper<>();
            if(roomSearchVO.getCityId()!=null){
                query.eq("city_id",roomSearchVO.getCityId());
            }
            if(roomSearchVO.getHotelId()!=null){
                query.eq("hotel_id",roomSearchVO.getHotelId());
            }
            if(roomSearchVO.getStatus()!=null){
                query.eq("status",roomSearchVO.getStatus());
            }
        query.eq("deleted",0);
            List<Room> rooms = roomService.list(query);
            if(rooms != null && rooms.size() > 0) {
                for(Room room :rooms){
                    RoomType rt = roomTypeService.getById(room.getRoomtypeId());
                    Floor f = floorService.getById(room.getFloorId());
                    City c = cityService.getById(room.getCityId());
                    HotelName h = hotelNameService.getById(room.getHotelId());

                    room.setStatusText(tDictService.getDictText(room.getStatus(),"hotelRoomStatus"));
                    room.setRoomTypeName(rt.getType());
                    room.setFloorName(f.getName());
                    room.setCityName(c.getCityName());
                    room.setHotelName(h.getHotelName());
                }
                try{
                    ExcelUtils.exportExcel(rooms, null, "房间数据", Room.class, "房间.xlsx", response);
                }catch (Exception e){
                    return Result.error(CodeError.INTERNAL_EXCEPTION,"导出失败");
                }
            }
            return Result.success("导出成功");
    }

    @ApiOperation("通过各种条件查询房间")
    @PostMapping("/search")
    public Result SearchRoomList(RoomSearchVO roomSearchVO){
        try{
            return roomService.searchRoomList(roomSearchVO);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


//    @ApiOperation("（建议去用search接口）查看特定状态下的房间列表接口-(传入状态值(0:空房,1:已入住,2:待退房,3:待打扫))")
//    @PostMapping("/screen")
//    public Result ScreenRoomList(@RequestParam(defaultValue = "1") Integer currentPage,
//                                 @RequestParam("status")Integer status){
//        return roomService.screenRoomList(currentPage,status);
//
//    }
//
//
//    @ApiOperation("（建议去用search接口）根据时间段显示可预定的房间")
//    @PostMapping("/bookableList")
//    public Result RoomBookableList(@RequestParam(defaultValue = "1") Integer currentPage,
//                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date checkinTime,
//                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date checkoutTime
//                                   ){
//        return roomService.BookableRoomList(currentPage,checkinTime,checkoutTime);
//    }



//    @ApiOperation("（建议去用search接口）获取房间列表接口")
//    @PostMapping("/list")
//    public Result RoomList(@RequestParam(defaultValue = "1") Integer currentPage){
//        return roomService.roomList(currentPage);
//    }

    @ApiOperation("添加房间接口")
    @PostMapping("/add")
    public Result AddRoom(@Valid RoomSaveVO roomSaveVO,BindingResult result){
        try{
            if (result.hasErrors()) {
                return Result.error(result.getFieldError().getDefaultMessage());
            }
            Room room = new Room();
            BeanUtils.copyProperties(roomSaveVO,room);

            QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("number",room.getNumber());
            queryWrapper.eq("hotel_id",room.getHotelId());
            queryWrapper.eq("city_id",room.getCityId());
            Room checkroom = roomService.getOne(queryWrapper);
            if(checkroom!=null){
                return Result.error(CodeError.ROOM_NUMBER_ALREADY,"该房间号已经存在🤬");
            }
            room.setIsDelete(0);
            boolean isAdd = roomService.save(room);
            if(isAdd){
                return Result.success("房间添加成功");
            }else{
                return Result.error(CodeError.ROOM_SAVE_FAIL,"房间添加失败,😭");
            }
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("修改房间信息接口")
    @PostMapping("/mod")
    public Result ModifyRoom(RoomViewVO room){
        Room newroom = new Room();
        BeanUtils.copyProperties(room,newroom);
        boolean isUpdate = roomService.updateById(newroom);
        if(isUpdate){
            return Result.success("房间信息修改成功");
        }else{
            return Result.error(CodeError.ROOM_MOD_FAIL,"房间信息修改失败,😭");
        }
    }

    @ApiOperation("删除房间信息接口")
    @PostMapping("/remove")
    public Result RemoveRoom(@RequestParam("id")Long id){
        Room room = roomService.getById(id);
        room.setIsDelete(1);
        boolean isRemove = roomService.updateById(room);
        if(isRemove){
            return Result.success("房间信息删除成功");
        }else{
            return Result.error(CodeError.ROOM_REMOVE_FAIL,"房间信息删除失败,😭");
        }
    }

    @ApiOperation("查询房间详情接口")
    @PostMapping("/view")
    public Result RoomView(@RequestParam("id")Long id){
        return roomService.roomVIew(id);
    }
}

